﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Text;
namespace syc.Sale
{
    public partial class SongJian : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String sql = @" select 原料到发明细.ID,dbo.原料供货单位.名称,dbo.原料.品名, dbo.原料.规格, dbo.原料.钢级,原料到发明细.炉号,原料到发明细.到货日期,
  dbo.原料到发明细.送检状态,dbo.原料到发明细.送检日期,dbo.原料到发明细.合格状态,dbo.原料到发明细.判定日期
,case when 原料到发明细.合格状态=1 and  原料到发明细.判定日期 IS not null then '合格'
when 原料到发明细.合格状态=0 and 原料到发明细.判定日期 IS not null then '不合格'
else  
'待判定' end as 状态描述
  FROM dbo.原料供货单位 INNER JOIN
  dbo.原料到发明细 on dbo.原料到发明细.供货ID=dbo.原料供货单位.ID
  INNER JOIN  dbo.原料 ON dbo.原料到发明细.原料ID = dbo.原料.ID ";


            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                string 部门 = Request.QueryString["depart"];
                if (部门 == "gongying")
                {
                    this.SqlDataSource1.SelectCommand = sql + " order by 到货日期 desc";
                    this.ViewState["SelectCommand"] = sql + " order by 到货日期 desc";

                    ListItem item1 = new ListItem();
                    item1.Text = "全部";
                    item1.Value = "0";
                    ListItem item2 = new ListItem();
                    item2.Text = "已送检已判定";
                    item2.Value = "1";
                    ListItem item3 = new ListItem();
                    item3.Text = "已送检合格";
                    item3.Value = "2";
                    ListItem item4 = new ListItem();
                    item4.Text = "已送检不合格";
                    item4.Value = "3";
                    ListItem item5 = new ListItem();
                    item5.Text = "已送检未判定";
                    item5.Value = "4";
                    ListItem item6 = new ListItem();
                    item6.Text = "未送检";
                    item6.Value = "5";
                    DPStatus.Items.Add(item1);
                    DPStatus.Items.Add(item2);
                    DPStatus.Items.Add(item3);
                    DPStatus.Items.Add(item4);
                    DPStatus.Items.Add(item5);
                    DPStatus.Items.Add(item6);
                    this.GridView1.Columns[10].Visible = false;
                    this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
                }
                else if (部门 == "zhijian")
                {
                    this.ToExcel.Visible = false;
                    this.YuanLiaoTitle.Text = "原料试样判定管理";
                    this.Panel1.Visible = false;
                    this.GridView1.Columns[7].Visible = false;
                    this.GridView1.Columns[8].Visible = false;
                    this.SqlDataSource1.SelectCommand = sql + " where dbo.原料到发明细.送检状态=1 " + " order by 到货日期 desc";
                    this.ViewState["SelectCommand"] = sql + " where dbo.原料到发明细.送检状态=1" + " order by 到货日期 desc";
                    ListItem item1 = new ListItem();
                    item1.Text = "全部";
                    item1.Value = "0";
                    ListItem item2 = new ListItem();
                    item2.Text = "合格";
                    item2.Value = "1";
                    ListItem item3 = new ListItem();
                    item3.Text = "不合格";
                    item3.Value = "2";
                    ListItem item4 = new ListItem();
                    item4.Text = "未判定";
                    item4.Value = "3";

                    DPStatus.Items.Add(item1);
                    DPStatus.Items.Add(item2);
                    DPStatus.Items.Add(item3);
                    DPStatus.Items.Add(item4);
                    this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];

                }
                else
                {
                    this.ToExcel.Visible = false;
                    this.YuanLiaoTitle.Text = "原料试样判定管理";
                    this.Panel1.Visible = false;
                    this.GridView1.Columns[7].Visible = false;
                    this.GridView1.Columns[8].Visible = false;
                    this.GridView1.Columns[10].Visible = false;
                    this.SqlDataSource1.SelectCommand = sql + " where dbo.原料到发明细.送检状态=1" + " order by 到货日期 desc";
                    this.ViewState["SelectCommand"] = sql + " where dbo.原料到发明细.送检状态=1" + " order by 到货日期 desc";
                    ListItem item1 = new ListItem();
                    item1.Text = "全部";
                    item1.Value = "0";
                    ListItem item2 = new ListItem();
                    item2.Text = "合格";
                    item2.Value = "1";
                    ListItem item3 = new ListItem();
                    item3.Text = "不合格";
                    item3.Value = "2";
                    ListItem item4 = new ListItem();
                    item4.Text = "未判定";
                    item4.Value = "3";

                    DPStatus.Items.Add(item1);
                    DPStatus.Items.Add(item2);
                    DPStatus.Items.Add(item3);
                    DPStatus.Items.Add(item4);
                    this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
                }
            }
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);

        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string temp = this.LuHao.Text.Trim().Replace("'", "");
            string Sql = "";
            //dbo.管材到货明细.管材类别=0 and 管材到货登记.质检次数=0 and 管材到货登记.质检结果=0 and
            Sql = "where  [原料到发明细].[炉号] like '%" + temp + "%'";
            //  Sql += " and [管材到货明细].[批次] like '%" + this.PiCi.Text + "%'";
            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                Sql += " and [原料到发明细].[送检日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                Sql += " and [原料到发明细].[送检日期]<='" + Dt.ToShortDateString() + "'";
            }
            if (this.GongHuo.Text != "0")
            {
                Sql += " and [原料供货单位].[名称]='" + this.GongHuo.SelectedItem.Text + "'";
            }
            if (this.ChanPin.Text != "0")
            {
                Sql += " and [原料].[ID]='" + this.ChanPin.SelectedItem.Value+ "'";
            }
            if (Request.QueryString["depart"] == "gongying")
            {
                if (this.DPStatus.Text != "0")
                {
                    if (this.DPStatus.Text == "1")
                    {
                        Sql += " and [原料到发明细].[送检状态]=1 and [原料到发明细].[判定日期] is not null";
                    }
                    if (this.DPStatus.Text == "2")
                    {
                        Sql += " and [原料到发明细].[送检状态]=1 and [原料到发明细].[合格状态]=1 and [原料到发明细].[判定日期] is not null";
                    }
                    if (this.DPStatus.Text == "3")
                    {
                        Sql += " and [原料到发明细].[送检状态]=1 and [原料到发明细].[合格状态]=0 and [原料到发明细].[判定日期] is not null";
                    }
                    if (this.DPStatus.Text == "4")
                    {
                        Sql += " and [原料到发明细].[送检状态]=1 and [原料到发明细].[合格状态]=0 and [原料到发明细].[判定日期] is  null";
                    }
                    if (this.DPStatus.Text == "5")
                    {
                        Sql += " and [原料到发明细].[送检状态]=0";
                    }
                }
            }
            else if (Request.QueryString["depart"] == "zhijian" || Request.QueryString["depart"] == "zhijianRead")
            {
                if (this.DPStatus.Text != "0")
                {
                    if (this.DPStatus.Text == "1")
                    {
                        Sql += " and [原料到发明细].[送检状态]=1 and [原料到发明细].[合格状态]=1 and [原料到发明细].[判定日期] is not null";
                    }
                    if (this.DPStatus.Text == "2")
                    {
                        Sql += " and [原料到发明细].[送检状态]=1 and [原料到发明细].[合格状态]=0 and [原料到发明细].[判定日期] is not null";
                    }
                    if (this.DPStatus.Text == "3")
                    {
                        Sql += " and [原料到发明细].[送检状态]=1 and [原料到发明细].[合格状态]=0 and [原料到发明细].[判定日期] is null";
                    }
                }
                else
                {
                    Sql += " and [原料到发明细].[送检状态]=1";
                }
            }
            this.SqlDataSource1.SelectCommand = @" select 原料到发明细.ID,dbo.原料供货单位.名称,dbo.原料.品名, dbo.原料.规格, dbo.原料.钢级,原料到发明细.炉号,原料到发明细.到货日期,
  dbo.原料到发明细.送检状态,dbo.原料到发明细.送检日期,dbo.原料到发明细.合格状态,dbo.原料到发明细.判定日期
,case when 原料到发明细.合格状态=1 and  原料到发明细.判定日期 IS not null then '合格'
when 原料到发明细.合格状态=0 and 原料到发明细.判定日期 IS not null then '不合格'
else  
'待判定' end as 状态描述
  FROM dbo.原料供货单位 INNER JOIN
  dbo.原料到发明细 on dbo.原料到发明细.供货ID=dbo.原料供货单位.ID
  INNER JOIN  dbo.原料 ON dbo.原料到发明细.原料ID = dbo.原料.ID  " + Sql + " order by 到货日期 desc";
            this.ViewState["SelectCommand"] = this.SqlDataSource1.SelectCommand;
        }
        //送检操作
        protected void SongJianChuLi_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[8].Controls[1];
                string ID = ((CheckBox)GridView1.Rows[i].Cells[8].Controls[1]).ToolTip;
                if (cb.Checked == true)
                {
                    this.SqlDataSource1.UpdateCommand = "Exec GuanCaiDaoHuo_MingXi_SongJian_Update " + "true," + ID;
                    this.SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.Text;
                    this.SqlDataSource1.Update();
                    this.GridView1.DataSourceID = this.SqlDataSource1.ID;
                    this.Message.Text = "·送检信息保存成功";
                }
                else if (cb.Checked == false)
                {
                    this.SqlDataSource1.UpdateCommand = "Exec GuanCaiDaoHuo_MingXi_SongJian_Update " + "false," + ID;
                    this.SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.Text;
                    this.SqlDataSource1.Update();
                    this.GridView1.DataSourceID = this.SqlDataSource1.ID;
                    this.Message.Text = "·送检信息保存成功";
                }
            }
        }

        protected void Button_Clear_Click(object sender, EventArgs e)
        {
            this.GongHuo.SelectedIndex = 0;
            this.ChanPin.SelectedIndex = 0;
            this.LuHao.Text = "";

            this.Date1.Text = "";
            this.Date2.Text = "";
        }
        protected void Check(string check, string ID)
        {

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            try
            {
                string op = (string)Session["OperatorName"];
                if (op == null)
                {
                    op = "";
                }
                Cmd.CommandText = "GuanCaiDaoHuo_MingXi_ZhiJian_Update";
                Cmd.CommandType = CommandType.StoredProcedure;
                Cmd.Parameters.Add(new SqlParameter("@是否合格", SqlDbType.Bit, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, check));
                Cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(ID)));
                Cmd.ExecuteNonQuery();
            }
            catch (Exception err)
            {
                this.Message.Text = err.Message;
            }
            Cmd.Dispose();
            Conn.Close();
            Conn.Dispose();
            // return (int)Cmd.Parameters["@返回值"].Value;
        }

        //合格判定
        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (Request.QueryString["depart"] != "gongying" )          
            {
                if(Request.QueryString["depart"] != "zhijianRead"){
                var ch = "";
                if (((CheckBox)sender).Checked)
                {
                    ch = "true";
                }
                else
                {
                    ch = "false";
                }
                var id = ((CheckBox)sender).ToolTip;
                Check(ch, id);
                this.Message.Text = "·判定成功";
                this.GridView1.SelectedIndex = -1;
                GridView1.DataBind();            
                }
            }
        }

        protected void ToExcel_Click(object sender, EventArgs e)
        {
            string part1 = @"<html xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:x='urn:schemas-microsoft-com:office:excel'
xmlns='http://www.w3.org/TR/REC-html40'>
<meta http-equiv=Content-Type content='text/html; charset=utf-8'>
<!--[if gte mso 9]><xml>
 <o:DocumentProperties>
  <o:Author>Chinese User</o:Author>
  <o:LastAuthor>Chinese User</o:LastAuthor>
  <o:LastPrinted>2010-07-08T09:01:11Z</o:LastPrinted>
  <o:Created>2010-07-08T02:03:52Z</o:Created>
  <o:LastSaved>2010-07-08T09:01:30Z</o:LastSaved>
  <o:Company>Chinese ORG</o:Company>
  <o:Version>11.9999</o:Version>
 </o:DocumentProperties>
</xml><![endif]-->
<style>
<!--table
	{mso-displayed-decimal-separator:'\.';
	mso-displayed-thousand-separator:'\,';}
@page
	{margin:.7in .75in 1.0in .74in;
	mso-header-margin:.5in;
	mso-footer-margin:.5in;}
tr
	{mso-height-source:auto;
	mso-ruby-visibility:none;}
col
	{mso-width-source:auto;
	mso-ruby-visibility:none;}
br
	{mso-data-placement:same-cell;}
.style0
	{mso-number-format:General;
	text-align:general;
	vertical-align:middle;
	white-space:nowrap;
	mso-rotate:0;
	mso-background-source:auto;
	mso-pattern:auto;
	color:windowtext;
	font-size:12.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	font-family:宋体;
	mso-generic-font-family:auto;
	mso-font-charset:134;
	border:none;
	mso-protection:locked visible;
	mso-style-name:常规;
	mso-style-id:0;}
.style21
	{color:blue;
	font-size:12.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:underline;
	text-underline-style:single;
	font-family:宋体;
	mso-generic-font-family:auto;
	mso-font-charset:134;
	mso-style-name:超链接;
	mso-style-id:8;}
a:link
	{color:blue;
	font-size:12.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:underline;
	text-underline-style:single;
	font-family:宋体;
	mso-generic-font-family:auto;
	mso-font-charset:134;}
a:visited
	{color:purple;
	font-size:12.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:underline;
	text-underline-style:single;
	font-family:宋体;
	mso-generic-font-family:auto;
	mso-font-charset:134;}
td
	{mso-style-parent:style0;
	padding-top:1px;
	padding-right:1px;
	padding-left:1px;
	mso-ignore:padding;
	color:windowtext;
	font-size:12.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	font-family:宋体;
	mso-generic-font-family:auto;
	mso-font-charset:134;
	mso-number-format:General;
	text-align:general;
	vertical-align:middle;
	border:none;
	mso-background-source:auto;
	mso-pattern:auto;
	mso-protection:locked visible;
	white-space:nowrap;
	mso-rotate:0;}
.xl24
	{mso-style-parent:style0;
	font-size:11.0pt;
	text-align:center;
	border:.5pt solid windowtext;}
.xl25
	{mso-style-parent:style0;
	font-size:11.0pt;
	text-align:center;
	border-top:.5pt solid windowtext;
	border-right:.5pt solid windowtext;
	border-bottom:.5pt solid windowtext;
	border-left:none;}
.xl26
	{mso-style-parent:style21;
	color:black;
	font-size:11.0pt;
	text-align:center;
	border-top:.5pt solid windowtext;
	border-right:.5pt solid windowtext;
	border-bottom:.5pt solid windowtext;
	border-left:none;}
.xl27
	{mso-style-parent:style21;
	color:black;
	font-size:11.0pt;
	text-align:center;
	border:.5pt solid windowtext;}
.xl28
	{mso-style-parent:style0;
	font-size:16.0pt;
	font-weight:700;
	text-align:center;
	border-top:none;
	border-right:none;
	border-bottom:.5pt solid windowtext;
	border-left:none;}
ruby
	{ruby-align:left;}
rt
	{color:windowtext;
	font-size:9.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	font-family:宋体;
	mso-generic-font-family:auto;
	mso-font-charset:134;
	mso-char-type:none;
	display:none;}
-->
</style>
<!--[if gte mso 9]><xml>
 <x:ExcelWorkbook>
  <x:ExcelWorksheets>
   <x:ExcelWorksheet>
    <x:Name>Sheet1</x:Name>
    <x:WorksheetOptions>
     <x:DefaultRowHeight>285</x:DefaultRowHeight>
     <x:Print>
      <x:ValidPrinterInfo/>
      <x:PaperSizeIndex>9</x:PaperSizeIndex>
      <x:HorizontalResolution>600</x:HorizontalResolution>
      <x:VerticalResolution>600</x:VerticalResolution>
     </x:Print>
     <x:Selected/>
     <x:Panes>
      <x:Pane>
       <x:Number>3</x:Number>
       <x:ActiveRow>6</x:ActiveRow>
       <x:ActiveCol>9</x:ActiveCol>
      </x:Pane>
     </x:Panes>
     <x:ProtectContents>False</x:ProtectContents>
     <x:ProtectObjects>False</x:ProtectObjects>
     <x:ProtectScenarios>False</x:ProtectScenarios>
    </x:WorksheetOptions>
   </x:ExcelWorksheet>
   <x:ExcelWorksheet>
    <x:Name>Sheet2</x:Name>
    <x:WorksheetOptions>
     <x:DefaultRowHeight>285</x:DefaultRowHeight>
     <x:ProtectContents>False</x:ProtectContents>
     <x:ProtectObjects>False</x:ProtectObjects>
     <x:ProtectScenarios>False</x:ProtectScenarios>
    </x:WorksheetOptions>
   </x:ExcelWorksheet>
   <x:ExcelWorksheet>
    <x:Name>Sheet3</x:Name>
    <x:WorksheetOptions>
     <x:DefaultRowHeight>285</x:DefaultRowHeight>
     <x:ProtectContents>False</x:ProtectContents>
     <x:ProtectObjects>False</x:ProtectObjects>
     <x:ProtectScenarios>False</x:ProtectScenarios>
    </x:WorksheetOptions>
   </x:ExcelWorksheet>
  </x:ExcelWorksheets>
  <x:WindowHeight>6750</x:WindowHeight>
  <x:WindowWidth>11700</x:WindowWidth>
  <x:WindowTopX>240</x:WindowTopX>
  <x:WindowTopY>105</x:WindowTopY>
  <x:ProtectStructure>False</x:ProtectStructure>
  <x:ProtectWindows>False</x:ProtectWindows>
 </x:ExcelWorkbook>
</xml><![endif]-->
</head>

<body link=blue vlink=purple>

<table x:str border=0 cellpadding=0 cellspacing=0 width=645 style='border-collapse:
 collapse;table-layout:fixed;width:485pt'>
 <col width=77 style='mso-width-source:userset;mso-width-alt:2464;width:58pt'>
 <col width=99 style='mso-width-source:userset;mso-width-alt:3168;width:74pt'>
 <col width=70 style='mso-width-source:userset;mso-width-alt:2240;width:53pt'>
 <col width=91 style='mso-width-source:userset;mso-width-alt:2912;width:68pt'>
 <col width=66 style='mso-width-source:userset;mso-width-alt:2112;width:50pt'>
 <col width=88 style='mso-width-source:userset;mso-width-alt:2816;width:66pt'>
 <col width=70 style='mso-width-source:userset;mso-width-alt:2240;width:53pt'>
 <col width=84 style='mso-width-source:userset;mso-width-alt:2688;width:63pt'>
 <tr height=43 style='mso-height-source:userset;height:32.25pt'>
  <td colspan=8 height=43 class=xl28 width=645 style='height:32.25pt;
  width:485pt'>试 样 结 果</td>
 </tr>
 <tr height=21 style='mso-height-source:userset;height:15.95pt'>
  <td height=21 class=xl24 style='height:15.95pt;border-top:none'>供货单位</td>
  <td class=xl25 style='border-top:none'>品名</td>
  <td class=xl25 style='border-top:none'>炉号</td>
  <td class=xl26 style='border-top:none'>规格</td>
  <td class=xl25 style='border-top:none'>是否送检</td>
  <td class=xl25 style='border-top:none'>送检日期</td>
  <td class=xl25 style='border-top:none'>是否合格</td>
  <td class=xl25 style='border-top:none'>判定日期</td>
 </tr>";
            string part3 = @"<![if supportMisalignedColumns]>
 <tr height=0 style='display:none'>
  <td width=77 style='width:58pt'></td>
  <td width=99 style='width:74pt'></td>
  <td width=70 style='width:53pt'></td>
  <td width=91 style='width:68pt'></td>
  <td width=66 style='width:50pt'></td>
  <td width=88 style='width:66pt'></td>
  <td width=70 style='width:53pt'></td>
  <td width=84 style='width:63pt'></td>
 </tr>
 <![endif]>
</table>

</body>

</html>";
            Response.Charset = "utf-8";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename=\"" + Server.UrlEncode("试样结果") + ".xls\"");
            Response.Write(part1);
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;

            string temp = this.LuHao.Text.Trim().Replace("'", "");
            string Sql = "";
            //dbo.管材到货明细.管材类别=0 and 管材到货登记.质检次数=0 and 管材到货登记.质检结果=0 and
            Sql = "where  [原料到发明细].[炉号] like '%" + temp + "%'";
            //  Sql += " and [管材到货明细].[批次] like '%" + this.PiCi.Text + "%'";
            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                Sql += " and [原料到发明细].[送检日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                Sql += " and [原料到发明细].[送检日期]<='" + Dt.ToShortDateString() + "'";
            }
            if (this.GongHuo.Text != "0")
            {
                Sql += " and [原料供货单位].[名称]='" + this.GongHuo.SelectedItem.Text + "'";
            }
            if (this.ChanPin.Text != "0")
            {
                Sql += " and [原料].[品名]='" + this.ChanPin.SelectedItem.Text + "'";
            }
            if (Request.QueryString["depart"] == "gongying")
            {
                if (this.DPStatus.Text != "0")
                {
                    if (this.DPStatus.Text == "1")
                    {
                        Sql += " and [原料到发明细].[送检状态]=1 and [原料到发明细].[判定日期] is not null";
                    }
                    if (this.DPStatus.Text == "2")
                    {
                        Sql += " and [原料到发明细].[送检状态]=1 and [原料到发明细].[合格状态]=1 and [原料到发明细].[判定日期] is not null";
                    }
                    if (this.DPStatus.Text == "3")
                    {
                        Sql += " and [原料到发明细].[送检状态]=1 and [原料到发明细].[合格状态]=0 and [原料到发明细].[判定日期] is not null";
                    }
                    if (this.DPStatus.Text == "4")
                    {
                        Sql += " and [原料到发明细].[送检状态]=1 and [原料到发明细].[合格状态]=0 and [原料到发明细].[判定日期] is  null";
                    }
                    if (this.DPStatus.Text == "5")
                    {
                        Sql += " and [原料到发明细].[送检状态]=0";
                    }
                }
            }
            else if (Request.QueryString["depart"] == "zhijian")
            {
                if (this.DPStatus.Text != "0")
                {
                    if (this.DPStatus.Text == "1")
                    {
                        Sql += " and [原料到发明细].[送检状态]=1 and [原料到发明细].[合格状态]=1 and [原料到发明细].[判定日期] is not null";
                    }
                    if (this.DPStatus.Text == "2")
                    {
                        Sql += " and [原料到发明细].[送检状态]=1 and [原料到发明细].[合格状态]=0 and [原料到发明细].[判定日期] is not null";
                    }
                    if (this.DPStatus.Text == "3")
                    {
                        Sql += " and [原料到发明细].[送检状态]=1 and [原料到发明细].[合格状态]=0 and [原料到发明细].[判定日期] is null";
                    }
                }
                else
                {
                    Sql += " and [原料到发明细].[送检状态]=1";
                }
            }
            string sql = @"  SELECT  dbo.原料供货单位.名称, dbo.原料.品名, dbo.原料.规格, dbo.原料到发明细.炉号,
                       dbo.原料到发明细.送检状态, dbo.原料到发明细.送检日期,dbo.原料到发明细.合格状态,dbo.原料到发明细.判定日期
                      FROM dbo.原料供货单位 INNER JOIN
                      dbo.原料到发明细 ON dbo.原料供货单位.ID = dbo.原料到发明细.供货ID INNER JOIN                    
                      dbo.原料 ON dbo.原料到发明细.原料ID = dbo.原料.ID " + Sql;
            Cmd.CommandText = sql;
            SqlDataReader Reader = Cmd.ExecuteReader();
            while (Reader.Read())
            {
                Response.Write("<tr height=21 style='mso-height-source:userset;height:15.95pt'><td height=21 class=xl27 style='height:15.95pt;border-top:none'><form id=form1 method=post name=form1 action='SongJian.aspx?depart=gongying'>"+Reader.GetString(0)+"</form></td>");
                Response.Write("<td class=xl24 style='border-top:none;border-left:none'><form id=form1 method=post name=form1 action='SongJian.aspx?depart=gongying'>" + Reader.GetString(1) + " </form></td>");
                Response.Write("<td class=xl24 style='border-top:none;border-left:none'><form id=form1 method=post name=form1 action='SongJian.aspx?depart=gongying'> " + Reader.GetString(3) + " </form></td>");
                Response.Write("<td class=xl24 style='border-top:none;border-left:none'>" + Reader.GetString(2) + "</td>");
                if (Reader.GetBoolean(4) == true)
                {
                    Response.Write("<td class=xl24 style='border-top:none;border-left:none'>已送检</td>");
                }
                else
                {
                    Response.Write("<td class=xl24 style='border-top:none;border-left:none'>未送检</td>");
                }
                if (Reader.IsDBNull(5))
                {
                    Response.Write("<td class=xl24 style='border-top:none;border-left:none'><form id=form1 method=post name=form1 action='SongJian.aspx?depart=gongying'></form></td>");
                }
                else
                {
                    Response.Write("<td class=xl24 style='border-top:none;border-left:none'><form id=form1 method=post name=form1 action='SongJian.aspx?depart=gongying'>" + Reader.GetDateTime(5).ToString("yyyy-MM-dd") + "</form></td>");

                }
                if (Reader.GetBoolean(6) == true && Reader.IsDBNull(7)==false)
                {
                    Response.Write("<td class=xl24 style='border-top:none;border-left:none'>合格</td>");
                }
                else if (Reader.GetBoolean(6) == false && Reader.IsDBNull(7) == false)
                {
                    Response.Write("<td class=xl24 style='border-top:none;border-left:none'>不合格</td>");
                }
                else
                {
                    Response.Write("<td class=xl24 style='border-top:none;border-left:none'>待检定</td>");
                }
                if (Reader.IsDBNull(7))
                {
                    Response.Write("<td class=xl24 style='border-top:none;border-left:none'><form id=form1 method=post name=form1 action='SongJian.aspx?depart=gongying'></form></td>");
                }
                else
                {
                    Response.Write("<td class=xl24 style='border-top:none;border-left:none'><form id=form1 method=post name=form1 action='SongJian.aspx?depart=gongying'>" + Reader.GetDateTime(7).ToString("yyyy-MM-dd") + "</form></td>");

                }
            }
            Reader.Close();
            Conn.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Dispose();
             Response.Write(part3);
            Response.End();
        }

    }
}
